module.exports = function(grunt) {
    // var script = require('grunt-cmd-transport/tasks/lib/script').init(grunt);
    // require('load-grunt-tasks')(grunt);
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        //合并文件
        concat: {
            options: {
                stripBanners: false,
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            cssConcat: {
                src: ['./src/common/css/style.css', './src/apps/<%= pkg.name %>/css/style.css'],
                dest: './dist/apps/<%= pkg.name %>/css/<%= pkg.name %>.css'
            },

            jsConcat: {
                src: ['dist/cache/<%= pkg.name %>/**/*.js'],
                dest: 'dist/apps/<%= pkg.name %>/js/<%= pkg.name %>.js'
            }
            

      
        },
        //压缩css
        cssmin: {
            options: {
                stripBanners: false,
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            build: {
                src: 'dist/apps/<%= pkg.name %>/css/<%= pkg.name %>.css',
                dest: 'dist/apps/<%= pkg.name %>/css/<%= pkg.name %>.min.css'
            }
        },
        //压缩js
        uglify: {
            options: {
                stripBanners: false,
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            build: {
                src: 'dist/apps/<%= pkg.name %>/js/<%= pkg.name %>.js',
                dest: 'dist/apps/<%= pkg.name %>/js/<%= pkg.name %>.min.js'
            }
          


        },
        //css检错
        // csslint: {
        //     options: {
        //         csslintrc: '.csslintrc'
        //     },
        //     lax: {
        //         src: ['build/css/*.css']
        //     }
        // },
        //js 检错
        jshint: {
            options: {
                jshintrc: '.jshintrc'
            },
            build: {
                src: ['src/apps/**/js/*.js','src/component/*.js','common/js/app.js'],
                //src: ['src/js/plug/**/*.js'],
                // afterconcat: ['dist/js/<%= pkg.name %>-<%= pkg.version %>.min.js']
            }
        },

        // 监听改动
        watch: {
            build: {
                files: ['src/apps/*/js/*.js', 'src/apps/*/template/**','src/component/**'],
                tasks: [ 'jshint',  'html2js_cmd'],
                options: {
                    spawn: false
                }
            }
        },
        //删除临时文件
        clean: {
            start:['./dist/apps/<%= pkg.name %>/**'],
            end:['./dist/cache/**']
        },
        
        //less 转 
        less: {
            compile: {
                files: {
                    'dist/css/<%= pkg.name %>-<%= pkg.version %>.css': 'src/css/*.css'
                }
            },
            pulg: {
                files: {
                    'plug/photo/photo.css': 'plug/photo/photo.css'
                }
            },
        },
        //压缩优化图片大小
        imagemin: {
            /* 压缩优化图片大小 */
            dist: {
                options: {
                    optimizationLevel: 7,
                    pngquant: true
                },
                files: [{
                    expand: true,
                    cwd: 'src/images/',
                    src: ['**/*.{png,jpg,jpeg,gif}'], // 优化 img 目录下所有 png/jpg/jpeg 图片
                    dest: 'dist/images/' // 优化后的图片保存位置，默认覆盖
                }]
            }
        },

       
        //html 转 amd 
        html2js_cmd: {
            mch: {
                files: [{
                    cwd: 'src/apps/mch/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/mch/template_js/'
                }]
            },
             component: {
                files: [{
                    cwd: 'src/component/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/component/template_js/'
                }]
            },
            wxCard: {
                files: [{
                    cwd: 'src/apps/wx-card/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/wx-card/template_js/'
                }]
            },
             user: {
                files: [{
                    cwd: 'src/apps/user/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/user/template_js/'
                }]
            },
             shop: {
                files: [{
                    cwd: 'src/apps/shop/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/shop/template_js/'
                }]
            },
            book: {
                files: [{
                    cwd: 'src/apps/book/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/book/template_js/'
                }]
            },
            takeout: {
                files: [{
                    cwd: 'src/apps/takeout/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/takeout/template_js/'
                }]
            },
             eat_in_hall: {
                files: [{
                    cwd: 'src/apps/eat_in_hall/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/eat_in_hall/template_js/'
                }]
            },
            oms: {
                files: [{
                    cwd: 'src/apps/oms/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/oms/template_js/'
                }]
            },
           shop_admin: {
                files: [{
                    cwd: 'src/apps/shop_admin/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/shop_admin/template_js/'
                }]
            },
            mch_mybank: {
                files: [{
                    cwd: 'src/apps/mch_mybank/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/mch_mybank/template_js/'
                }]
            }, 
            community: {
                files: [{
                    cwd: 'src/apps/community/template/',
                    src: '**/*.html',
                    expand: true,
                    ext: '.js',
                    dest: 'src/apps/community/template_js/'
                }]
            }
        },

        //cmd规范文件转换
        transport: {
            js: {
                options: {
                    idleading: './js/',
                    debug: false,
                },
                files: [{
                    cwd: './dist/cache/<%= pkg.name %>/babeljs',
                    src: '**/*.js',
                    expand: true,
                    dest: './dist/cache/<%= pkg.name %>/js'
                }]
            },
            template: {
                options: {
                    idleading: './template_js/',
                    debug: false,
                },
                files: [{
                    cwd: './src/apps/<%= pkg.name %>/template_js',
                    src: '**/*.js',
                    expand: true,
                    dest: './dist/cache/<%= pkg.name %>/template_js'
                }]
            },
            appjs: {
                options: {
                    idleading: '/common/js/',
                    debug: false,
                },
                files: [{
                    cwd: './src/common/js',
                    src: 'app.js',
                    expand: true,
                    dest: './dist/cache/<%= pkg.name %>'
                }]
            }
        },
        
        copy: {
            dist: {
                expand: true,
                cwd: 'src/plug',
                src: '**',
                dest: 'dist/plug',
            },
            component: {
                expand: true,
                cwd: 'src/component',
                src: '**',
                dest: 'dist/component',
            },
            css: {
                expand: true,
                cwd: 'src/apps/<%= pkg.name %>/css',
                src: '**',
                dest: 'dist/apps/<%= pkg.name %>/css',
            },

            images: {
                expand: true,
                cwd: 'src/apps/<%= pkg.name %>/images',
                src: '**',
                dest: 'dist/apps/<%= pkg.name %>/images',
            }
        },

        babel: {
            options: {
                sourceMap: false,
                presets: ['babel-preset-es2015']
            },
            build: {
                files: [{
                    expand: true,
                    cwd: './src/apps/<%= pkg.name %>/js',
                    src: ['**/*.js'],
                    dest: './dist/cache/<%= pkg.name %>/babeljs'
                }]
            }
        },
          //文件内替换
        anyreplace: {
            usemin: {
                options: {
                    timestamp: false,
                    replacements: [{
                        from: /{version}/ig,
                        to: '<%= pkg.version %>'
                    }]
                },
                files: [{
                    expand: false,
                    // cwd: './src/apps/<%= pkg.name %>/index._dist.html',
                    src: ['./src/apps/<%= pkg.name %>/index_dist.html'],
                    dest: './dist/apps/<%= pkg.name %>/index.html'
                }]
            }
        },



    });

    // 加载包含 "concat" 合并文件
    grunt.loadNpmTasks('grunt-contrib-concat');

    // 加载包含压缩css
    grunt.loadNpmTasks('grunt-contrib-cssmin');

    // 加载包含压缩js
    grunt.loadNpmTasks('grunt-contrib-uglify');

    //检查css 
    //grunt.loadNpmTasks('grunt-contrib-csslint');

    //检查js 
    grunt.loadNpmTasks('grunt-contrib-jshint');

    //监听css js 变化 
    grunt.loadNpmTasks('grunt-contrib-watch');

    //清文件
    grunt.loadNpmTasks('grunt-contrib-clean');

   

    //less 
    grunt.loadNpmTasks('grunt-contrib-less');

    //less 语法检查
     grunt.loadNpmTasks('grunt-lesslint')
    //文件
    //grunt.loadNpmTasks('grunt-asset-injector');

    //图片压缩
    grunt.loadNpmTasks('grunt-contrib-imagemin');

    //grunt.loadNpmTasks('grunt-html2js');
    //html2amd
    // grunt.loadNpmTasks('grunt-html2amd');
    grunt.loadNpmTasks('grunt-html2js-cmd');
    //cmd
    grunt.loadNpmTasks('grunt-cmd-transport');
    //anyreplace
    grunt.loadNpmTasks('grunt-anyreplace');
    grunt.loadNpmTasks('grunt-babel');
    //grunt.loadNpmTasks('babel-preset-es2015');
    //grunt.loadNpmTasks('grunt-datauri');
    grunt.loadNpmTasks('grunt-contrib-copy');
    // grunt.loadNpmTasks('grunt-css-encode');
    // 默认被执行的任务列表。
    grunt.registerTask('dist', ["clean:start",  'jshint', 'babel',  'html2js_cmd', 'copy', 'transport', 'concat', 'cssmin', 'uglify','anyreplace',"clean:end"]);
    grunt.registerTask('default', [  'jshint',  'html2js_cmd',   'watch']);
    grunt.registerTask('test', [ 'concat','cssmin']);


    
};